﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;

namespace Ruandao.MiniFace
{
    public static class RenderSyntaxHighlight
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="page"></param>
        /// <param name="code">使用pre标记时应用了html编码，使用script时用&lt;![CDATA[ ]]&gt;包住代码段。</param>
        /// <param name="brush">代码的格式类型，如：js、</param>
        /// <param name="isPreTag">是否使用pre标记，否则使用script标记。</param>
        /// <returns></returns>
        public static string ReferenceSyntaxHighlight(this System.Web.UI.Page page, string shUrl)
        {
            if (!page.Items.Contains("SyntaxHightlightClientScript"))
            {
                page.Items.Add("SyntaxHightlightClientScript", true);
                StringBuilder script = new StringBuilder();

                /* 待解决问题：src版本不能正常运行，所以先把它注释掉 */
                //#if(!DEBUG)
                script.Append(string.Format("\n<script type='text/javascript' src='{0}scripts/shCore.js'></script>", shUrl));
                script.Append(string.Format("\n<script type='text/javascript' src='{0}scripts/shAutoloader.js'></script>", shUrl));
                //#else       
                //                script.Append(string.Format("<script type='text/javascript' src='{0}src/shCore.js'></script>",path));
                //                script.Append(string.Format("<script type='text/javascript' src='{0}src/shAutoloader.js'></script>", path));
                //#endif

                return script.ToString();
            }
            return "";
        }
        public static string SyntaxHighlightHtml(this System.Web.UI.Page page, string code, string brush, bool isPreTag)
        {
            StringBuilder sb = new StringBuilder();
            string text = page.Server.HtmlEncode(code);
            if (isPreTag)
            {
                sb.Append(string.Format("<pre class='brush: {0}'>{1}</pre>", brush, text));
            }
            else
            {
                sb.Append(string.Format("<script type='syntaxhighlighter' class='brush: {0}'><![CDATA[{1}]]></script>", brush, text));
            }
            return sb.ToString();
        }
    }
}
